import { reactive } from "vue";
import type { FormRules } from "element-plus";
import { ValidateIdentity, ValidatePhone } from "@/utils/validate";

/** 自定义表单规则校验 */
export const formRules = reactive(<FormRules>{
  name: [
    { required: true, message: "姓名为必填项", trigger: "blur" },
    { max: 20, message: "姓名不能超过20字", trigger: "blur" }
  ],
  idCard: [
    { required: true, message: "证件号为必填项", trigger: "blur" },
    { max: 18, message: "证件号不能超过18位", trigger: "blur" },
    {
      trigger: "blur",
      validator: (_, value, callback) => {
        const res = ValidateIdentity.IdentityCodeValid(value);
        if (res == true) {
          callback();
        } else {
          callback(new Error(res));
        }
      }
    }
  ],
  phone: [
    { required: true, message: "手机号为必填项", trigger: "blur" },
    { len: 11, message: "手机号必须为11位", trigger: "blur" },
    {
      message: "手机号格式错误",
      trigger: "blur",
      validator: (_, value, callback) => {
        if (ValidatePhone(value)) {
          callback();
        } else {
          callback(new Error("手机号格式错误"));
        }
      }
    }
  ],
  gender: [{ required: true, message: "性别为必填项", trigger: "blur" }],
  deptId: [{ required: true, message: "所属部门为必填项", trigger: "blur" }],
  workId: [{ required: true, message: "工号类型为必填项", trigger: "blur" }],
  jobId: [{ required: true, message: "职务为必填项", trigger: "blur" }],
  education: [{ required: true, message: "学历为必填项", trigger: "blur" }],
  salary: [
    { required: true, message: "薪资为必填项", trigger: "blur" },
    {
      trigger: ["change", "blur"],
      validator: (_, value, callback) => {
        if (Number(value) !== 0) {
          callback();
        } else {
          callback(new Error("请输入薪资"));
        }
      }
    }
  ]
});
